﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using CellarDoor.Gui.Theming;
using CellarDoor.Gui.Layout;

namespace CellarDoor.Gui.Elements
{
    /// <summary>
    /// A progress bar that can display a progress state.
    /// </summary>
    public class UiProgressBar : UiElement
    {
        private float value;

        /// <summary>
        /// Gets or sets the value.
        /// The value is clamped to [0, 1]
        /// </summary>
        public float Value
        {
            get { return value; }
            set
            {
                this.value = MathHelper.Clamp(value, 0, 1);
                OnValueChanged();
            }
        }

        /// <summary>
        /// Occurs when value has changed.
        /// </summary>
        public event EventHandler ValueChanged;

        /// <summary>
        /// Creates a progress bar
        /// </summary>
        public UiProgressBar()
            : base()
        {
            Initialize();
        }

        /// <summary>
        /// Creates a progress bar
        /// </summary>
        public UiProgressBar(UiElement parent)
            : base(parent)
        {
            Initialize();
        }

        private void Initialize()
        {
            base.Style = new UiVisualStyleProgress(this);
            ElementName = "ProgressBarH";
        }

        private void OnValueChanged()
        {
            UiVisualStyleProgress s = Style as UiVisualStyleProgress;
            if (s != null)
            {
                s.Progress = Value;
                this.InvalidateStyle();
            }
            if (ValueChanged != null)
            {
                ValueChanged(this, null);
            }
        }
    }
}
